/**
 * Copyright (c) 2011-2014, junchen (homeanter@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package xin.tianhui.cloud.weblog.web.api;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;
import xin.tianhui.cloud.weblog.common.model.dto.CommentDTO;

/**
 * @author junchen homeanter@163.com
 * @Data 2018-04-11 15:32:38
 */
@Slf4j
@RestController
@RequestMapping("api")
public class CommentController {
	
	@Autowired
	private ObjectMapper mapper;
	
	/**
	 * 评论同步
	 * @param data void
	 * @author junchen homeanter@163.com
	 * @Data 2018-04-11 15:38:16
	 */
	@SuppressWarnings("unchecked")
	@PostMapping("sync/comment")
	public void syncComment(String data) {
		try {
			if(StringUtils.isNotBlank(data)) {
				Map<String,Object> map = mapper.readValue(data, HashMap.class);
				List<Map<String,Object>> commentsMap = (List<Map<String, Object>>) map.get("comments");
				List<CommentDTO> comments = Optional.ofNullable(commentsMap).map(result->{
					List<CommentDTO> commentList = Lists.newArrayList(); 
					result.forEach(r->{
						CommentDTO comment = new CommentDTO();
						comment.setBid(MapUtils.getInteger(r,"cmtid"));
						Date date = new Date();
						date.setTime(MapUtils.getLong(map,"ttime"));
						comment.setCreateTime(date);
						comment.setChannelId(MapUtils.getInteger(map,"sourceid"));
						comment.setArticleId(MapUtils.getInteger(map, "categoryid"));
						comment.setContent(MapUtils.getString(r, "content"));
						Map<String,Object> userMap = (Map<String, Object>) r.get("user");
						comment.setNickname(MapUtils.getString(userMap, "nickname"));
						comment.setUserIcon(MapUtils.getString(userMap, "usericon"));
						comment.setType(MapUtils.getInteger(r, "channeltype"));
						commentList.add(comment);
					});
					return commentList;
				}).orElse(null);
				if(CollectionUtils.isNotEmpty(comments)) {
					log.debug("data:{}",comments);
				}
			}
		} catch (IOException e) {
			log.error("同步评论失败:{}",e);
		}
	}
}
